%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% createMissingDirs.m
% ffmpeg
%
%==========================================================================

%==========================================================================
% The function "getImagesFromImages" converts specified ranges of given
% image files to a different grayscale image format and stores the results
% in new image files with a desired file name pattern.
% INPUT: "inFileNamePrefix" is a string specifying a common prefix
% (including a full directory path) for the image files to be converted.
% "inIndFieldLength" is an optional parameter specifying the number of
% digits used to specify the index of an image in its file name. Image
% indices are specified with leading zeros. For example, if the index field
% length is 6, then the index of image 241 is specified as 000241 in its
% file name.
% "inFileExt" is a string specifying the image file format of the image
% files to be converted.
% "fileRangesArr" is a 2D array of two columns and many rows. Each row
% represents a range of integers specifying the indices of the image files
% to be converted.
% "outFileNamePrefix" is a string specifying a common prefix (including a
% full directory path) for the converted image files.
% "outIndFieldLength" is an optional parameter specifying the number of
% digits used to specify the index of a converted image in its file name.
% Image indices are specified with leading zeros. For example, if the index
% field length is 6, then the index of image 241 is specified as 000241 in
% its file name.
% "outFileExt" is an optional string specifying an image file format for
% the converted image files.
% OUTPUT: None.
%==========================================================================
function getImagesFromImages(inFileNamePrefix, inIndFieldLength, inFileExt, ...
    fileRangesArr, outFileNamePrefix, outIndFieldLength, outFileExt)

if (nargin < 6)
    outIndFieldLength = 6;
end
if (nargin < 7)
    outFileExt = 'pgm';
end

% Create any missing directories and get the directory where the output
% image files are to be written.
scratchDir = createMissingDirs(outFileNamePrefix);
    
% Set up the format strings for the files' index field.
inFormatStr = ['%0' num2str(inIndFieldLength) 'd'];
outFormatStr = ['%0' num2str(outIndFieldLength) 'd'];

% Compute the total number of image files to be converted.
numFiles = sum(fileRangesArr(:, 2) - fileRangesArr(:, 1) + 1);
% Allocate a cell array for the strings holding the individual ffmpeg calls
% converting the image files.
ffmpegCallsCellArr = cell(numFiles, 1); 
currFfmpegCallInd = 0;

% Convert the image files whose indices are in the specified ranges.
for ind = 1:size(fileRangesArr, 1)
    
    % Compute number of files in the current range.
    numFilesInRange = fileRangesArr(ind, 2) - fileRangesArr(ind, 1) + 1;
    
    for k = 1:numFilesInRange
        % Compute the current image file's index.
        fileInd = k + fileRangesArr(ind, 1) - 1;
        % Construct the name of the image file to be converted.
        inFileName = [inFileNamePrefix num2str(fileInd, inFormatStr) '.' inFileExt];
        % Construct a name for the converted image file.
        outFileName = [outFileNamePrefix num2str(fileInd, outFormatStr) '.' outFileExt];
        
        % Increment the index of the cell array.
        currFfmpegCallInd = currFfmpegCallInd + 1;
        % Store the current ffmpeg call to the cell array of calls.
        ffmpegCallsCellArr{currFfmpegCallInd} = ['ffmpeg -i "' inFileName '" -pix_fmt gray16be "' outFileName '"'];
    end
end

% Create a name for a batch file.
batchFileName = [scratchDir 'ffmpegCalls.bat'];
% Open the batch file for writing.
fid = fopen(batchFileName, 'w');
% Write all ffmpeg calls to the batch file, one call per line.
fprintf(fid, '%s\n', ffmpegCallsCellArr{:});
% Close the file.
fclose(fid);

% Run the batch file that will store all frames in Matlab-readable image
% files.
system(batchFileName);

% Get rid of the temporary batch file.
delete(batchFileName);

return;
% end of the function "getImagesFromImages"
